package com.itheima.service.impl;

import com.itheima.anno.Log;
import com.itheima.exception.BusinessException;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Dept;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Service
public class DeptServiceimpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;
    @Override
    public List<Dept> selectAll() {
        return deptMapper.selectAll();
    }

    @Override
    @Log
    public void deletById(Integer id) {
        //根据id查询员工
        Integer deptId = id;
        long count =  empMapper.selectByEmpId(deptId);
        if(count>0){
            throw new BusinessException("异常");
        }else {
            deptMapper.deleteById(id);
        }

    }

    @Override
    @Log
    public void add(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        dept.setCreateTime(LocalDateTime.now());
        deptMapper.add(dept);
    }

    @Override
    public Dept selectById(Integer id) {
        return deptMapper.selectById(id);
    }

    @Override
    @Log
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }
}
